package com.kylexy.closes.widge.photo;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.androidquery.AQuery;
import com.kylexy.closes.R;
import com.kylexy.closes.app.AppContext;
import com.kylexy.closes.app.UIHelper;
import com.tl.common.util.DebugUtil;
import com.tl.common.util.StringUtils;

public class ShowPicture extends RelativeLayout {
	AQuery mAquery;
	String mImgUrl;
	String mImgName;
	Context mContext;
	View mView;

	PopupWindowPicZoom mWindowShowPic;

	Boolean mShowBigView = true;
	int mDefautBackground = R.drawable.ic_add_smiley;

	int mHeight = 120;
	int mWidth = 120;

	ImageView mivPic;

	String _videPath;

	public ShowPicture(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initView(context);
	}

	public ShowPicture(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initView(context);
		// TODO Auto-generated constructor stub
	}

	private void initView(Context context) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mView = inflater.inflate(R.layout.layout_single_picture, this);
		mView.setFocusable(false);

		mivPic = (ImageView) findViewById(R.id.single_picture_pic);

		mAquery = new AQuery(this);

		mivPic.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				beginPic(v);
			}
		});

		mivPic.setBackgroundResource(mDefautBackground);
	}

	public void showImage(String fileName) {
		String appPath = AppContext.getInstance()._appImagePath;
		if (appPath == "")
			return;

		String imgFile = appPath + fileName;

		File origFile = new File(imgFile);
		boolean origExits = (imgFile != null && origFile.exists());
		if (origExits) {
			mImgUrl = appPath + fileName;
			mImgName = fileName;
			showPic();
		} else {
			DebugUtil.printMethodTag("图片地址:" + mImgUrl + "未找到图片!");
		}
	}

	public void showImage(String path, String fileName) {

		String imgFile = path + fileName;
		File origFile = new File(imgFile);
		boolean origExits = (imgFile != null && origFile.exists());
		if (origExits) {
			mImgUrl = path + fileName;
			mImgName = fileName;
			showPic();
		}
	}

	/**
	 * 上传成功后图片
	 * 
	 * @param url
	 */
	private void showPic() {

		// mAquery.id(R.id.single_picture_pic).clicked(this);

		mWidth = mView.getMeasuredWidth();
		mHeight = mView.getMeasuredHeight();

		DebugUtil.printMethodTag("图片地址:" + mImgUrl);

		File origFile = new File(mImgUrl);
		boolean origExits = (mImgUrl != null && origFile.exists());
		if (origExits) {

			mAquery.id(R.id.single_picture_pic)
					.progress(R.id.single_picture_progress)
					.image(mImgUrl, false, true);

			return;
		}

	}

	private void beginPic(View v) {

		// 关闭输入法
		UIHelper.HideKeyBoard(mContext, v);

		// 打开浏览窗口
		if (StringUtils.isEmpty(_videPath)) {
			if (!StringUtils.isEmpty(mImgUrl)) {
				mWindowShowPic = new PopupWindowPicZoom((Activity) mContext,
						mImgUrl);
				mWindowShowPic.showAtLocation(v, Gravity.CENTER, 0, 0);
			}
		} else {

			Intent intent = new Intent();
			File f = new File(_videPath);
			if (f.exists()) {
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 开新的任务
				intent.setAction(android.content.Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.fromFile(f), "video/*");
				mContext.startActivity(intent);
				intent = null;
			}

		}

	}

	public void showVideo(String veidoPath) {
		String appPath = AppContext.getInstance()._appImagePath;
		if (appPath == "")
			return;

		String path = appPath + veidoPath;
		File file = new File(path);
		if (file.exists()) {

			Bitmap bt = ThumbnailUtils.createVideoThumbnail(path,
					ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

			if (bt != null) {
				mivPic.setImageBitmap(bt);
			} else {
				mivPic.setBackgroundResource(mDefautBackground);
			}

			_videPath = path;

		}
	}
}
